function Music(src) {
    Object.defineProperties(this, {
        musics: { // 储存对象
            value: null,
            writable: true
        },
        plays: { // 储存立即播放的对象
            value: [],
            writable: true
        },
        _offMusic: { // 关闭音乐  false为不关闭， true为关闭
            value: false,
            writable: true
        },
        offMusic: {
            get() {
                return this._offMusic;
            },
            set(value) {
                this._offMusic = value;
                window.top.WeixinJSBridge && window.top.WeixinJSBridge.invoke('getNetworkType', {}, (e)=>{
                    if (value) {
                        this.musics.map((todo, i) => {
                            todo.dom.pause();
                            // todo.dom.volume = 0;
                        });
                    } else {
                        this.plays.map((todo, i) => {
                            todo.play();
                        });
                        /* this.musics.map((todo, i)=>{
                            todo.dom.volume = todo.volume;
                        }); */
                    }
                })
            }
        }
    });
    this.constructor(src);
}

Music.prototype = {
    constructor(src) {// 初始化实例到dom
        var _self = this;
        this.musics = src.map(function (todo, i) {
            var audio = document.createElement("audio");
            audio.id = todo.name;
            audio.src = todo.src;
            audio.jump = function (name) {
                _self.jump.call(this, _self, name);
            };
            todo.loop && (audio.loop = true);
            todo.volume && (audio.volume = todo.volume);
            todo.play && _self.plays.push(audio);
            audio.play = function () {// 重置play
                if (_self.offMusic) {
                    return;
                }
                this.currentTime = 0;
                var _this = this;
                if (typeof WeixinJSBridge == "undefined" && false) {
                    HTMLAudioElement.prototype.play.call(_this)
                } else {
                    var a = setInterval(function () {
                        window.top.WeixinJSBridge && (clearInterval(a), window.top.WeixinJSBridge.invoke('getNetworkType', {}, function (e) {
                            HTMLAudioElement.prototype.play.call(_this)
                        }))
                    }, 10)
                }
            };
            audio.continued = function(){// 添加音乐续的方法
                if (_self.offMusic) {
                    return;
                }
                var _this = this;
                if (typeof WeixinJSBridge == "undefined"){
                    HTMLAudioElement.prototype.play.call(_this)
                }else{
                    var a = setInterval(function () {
                        window.top.WeixinJSBridge && (clearInterval(a), window.top.WeixinJSBridge.invoke('getNetworkType', {}, function (e) {
                            HTMLAudioElement.prototype.play.call(_this)
                        }))
                    }, 10)
                }  
            }
            document.body.appendChild(audio);
            return {
                dom: audio,
                src: todo.src,
                name: todo.name,
                stage: todo.stage,
                volume: todo.volume?todo.volume:1,
                getSeek: function getSeek(name) {
                    return _self.getSeek.call(this, _self, name);
                }
            };
        });
        window.onload = function () {
            _self.plays.map(function (todo, i) {
                todo.play();
            });
        }
        this.init();
    },
    getAudio(name) {// 获取指定音乐
        var i = 0;
        for (i; i < this.musics.length; i++) {
            if (this.musics[i].name == name) return this.musics[i];
        }
    },
    getSeek(_self, name) {// 获取指定音乐段
        var i = 0;
        for (i; i < this.stage.length; i++) {
            if (this.stage[i].name == name) {
                return this.stage[i].time;
            }
        }
        console.error(this.name + "未定义" + name + "跳转");
    },
    jump(_self, name) {// 跳转到指定音乐
        var music = _self.getAudio(this.id);
        if (!music.stage) {
            console.error(this.id + "音乐未自定义跳转");
            return;
        }
        music.dom.play();
        music.dom.currentTime = music.getSeek("betting");
    },
    init() {// 初始
        console.log("进入初始");
    }
};


export default Music;